﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.enums;
using FisApp.utility;
using System.Data.SqlClient;

namespace FisApp
{
    public partial class FrmMakineSarac : Form
    {
        #region Variables...
        private string makineSaracId;
        private string sqlCommandType = SqlCommandType.INSERT;

        public string makineSaracKodu
        {
            get
            {
                return txtMakineSaracKodu.Text;
            }
            set
            {
                txtMakineSaracKodu.Text = value;
            }
        }

        public string makineSaracAciklama
        {
            get
            {
                return txtMakineSaracAciklama.Text;
            }
            set
            {
                txtMakineSaracAciklama.Text = value;
            }
        }

        public DataTable makineSaraclar
        {
            get
            {
                return (DataTable)gcMakineSarac.DataSource;
            }

            set
            {
                gcMakineSarac.DataSource = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                return gvMakineSarac.GetDataRow(gvMakineSarac.FocusedRowHandle);
            }
        }
        #endregion

        #region Constructor...
        public FrmMakineSarac()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods...
        private void clearForm()
        {
            makineSaracId = null;
            makineSaracKodu = null;
            makineSaracAciklama = null;
            btnVazgec.Visible = false;
            sqlCommandType = SqlCommandType.INSERT;
        }

        private void LoadMakineSaraclar()
        {
            try
            {
                string query = "SELECT * FROM makinesarac";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                    makineSaraclar = ds.Tables[0];
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmMakineSarac_Load(object sender, EventArgs e)
        {
            LoadMakineSaraclar();
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(makineSaracKodu))
            {
                FrmMessage.showDialog("Lütfen makina saraç kodu giriniz.", MessageTypes.Warning);
                return;
            }
            if (string.IsNullOrEmpty(makineSaracAciklama))
            {
                FrmMessage.showDialog("Lütfen makina saraç açıklama giriniz.", MessageTypes.Warning);
                return;
            }

            try
            {
                if (sqlCommandType.Equals(SqlCommandType.INSERT))
                {
                    string command = "INSERT INTO makinesarac (makinesarac_kodu, makinesarac_aciklama) VALUES (@makinesarac_kodu, @makinesarac_aciklama)";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@makinesarac_kodu", makineSaracKodu));
                    list.Add(DBUtils.GetParameter("@makinesarac_aciklama", makineSaracAciklama));
                    DBUtils.ExecuteSQLCommand(command, list);
                }
                else if (sqlCommandType.Equals(SqlCommandType.UPDATE))
                {
                    string command = "UPDATE makinesarac SET makinesarac_kodu=@makinesarac_kodu, makinesarac_aciklama=@makinesarac_aciklama WHERE makinesarac_id=@makinesarac_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@makinesarac_kodu", makineSaracKodu));
                    list.Add(DBUtils.GetParameter("@makinesarac_aciklama", makineSaracAciklama));
                    list.Add(DBUtils.GetParameter("@makinesarac_id", makineSaracId));
                    DBUtils.ExecuteSQLCommand(command, list);
                }
                LoadMakineSaraclar();
                clearForm();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Kayıt işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void repbtnEdit_Click(object sender, EventArgs e)
        {
            makineSaracId = SelectedRow["makinesarac_id"].ToString();
            makineSaracKodu = SelectedRow["makinesarac_kodu"].ToString();
            makineSaracAciklama = SelectedRow["makinesarac_aciklama"].ToString();
            sqlCommandType = SqlCommandType.UPDATE;
            btnVazgec.Visible = true;
        }

        private void repbtnDelete_Click(object sender, EventArgs e)
        {
            makineSaracId = SelectedRow["makinesarac_id"].ToString();

            try
            {
                if (FrmMessage.ShowDialogResult(SelectedRow["makinesarac_kodu"].ToString() + " kod'lu makine saracı bilgisini silmek istediğinize emin misiniz ?", MessageTypes.Question) == DialogResult.Yes)
                {
                    string command = "DELETE FROM makinesarac WHERE makinesarac_id=@makinesarac_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@makinesarac_id", makineSaracId));
                    DBUtils.ExecuteSQLCommand(command, list);

                    LoadMakineSaraclar();
                    clearForm();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().Contains("-2146232060"))
                    FrmMessage.showDialog(General.ForeignKeyExceptionMessage, MessageTypes.Error);
                else
                    FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);

                //FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void btnVazgec_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void FrmMakineSarac_Activated(object sender, System.EventArgs e)
        {
            FrmMakineSarac_Load(null, null);
        }
        #endregion

    }
}
